<?php

/**
 * Admin App Controller
 *
 * PHP 5
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the below copyright notice.
 *
 * @author     Yusuf Abdulla Shunan <shunan@maldicore.com>
 * @copyright  Copyright 2012, Maldicore Group Pvt Ltd. (http://maldicore.com)
 * @license    MIT License (http://www.opensource.org/licenses/mit-license.php)
 * @since      CakePHP(tm) v 2.1.1
 */
App::uses('AppController', 'Controller');
App::uses('Folder', 'Utility');

class AdminAppController extends AppController {

    public function beforeRender() {
        parent::beforeRender();
        $navbar = array();
        $navbar[] = array(
            'title' => 'Admin users',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'admin_users',
                'action' => 'index',
            ),
            'icon' => 'assistant'
        );
        $navbar[] = array(
            'title' => 'Users',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'users',
                'action' => 'index',
            ),
            'icon' => 'user'
        );

        $navbar[] = array(
            'title' => 'Payment',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'authorize_settings',
                'action' => 'index',
            ),
            'icon' => 'user',
            'sub' => array(
                array(
                    'title' => 'Authorize.net Settings',
                    'url' => array(
                        'plugin' => 'admin',
                        'controller' => 'authorize_settings',
                        'action' => 'index',
                    ),
                    'icon' => 'user'
                ),
                array(
                    'title' => 'Transactions',
                    'url' => array(
                        'plugin' => 'admin',
                        'controller' => 'transactions',
                        'action' => 'index',
                    ),
                    'icon' => 'user'
                )
            )
        );

        $navbar[] = array(
            'title' => 'Contacts',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'contacts',
                'action' => 'index',
            ),
            'icon' => 'user'
        );

        $navbar[] = array(
            'title' => 'Settings',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'settings',
                'action' => 'index',
            ),
            'icon' => 'background'
        );

        $navbar[] = array(
            'title' => 'CMS pages',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'cmspages',
                'action' => 'index',
            ),
            'icon' => 'page'
        );

        $navbar[] = array(
            'title' => 'Companies',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'companies',
                'action' => 'index',
            ),
            'icon' => 'background'
        );

        $navbar[] = array(
            'title' => 'Brokers',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'brokers',
                'action' => 'index',
            ),
            'icon' => 'categories'
        );

        $navbar[] = array(
            'title' => 'Stocks',
            'url' => array(
                'plugin' => 'admin',
                'controller' => 'stocks',
                'action' => 'index',
            ),
            'icon' => 'category'
        );

        $this->set(compact('navbar'));
        $this->layout = 'default';
    }

    function authenticate() {
        // Check if the session variable User exists, redirect to loginform if not
        if (!$this->Session->check('User')) {
            $this->redirect(array('controller' => 'users', 'action' => 'login_form'));
            exit();
        }
    }

    // Authenticate on every action, except the login form
    function afterFilter() {
        if ($this->action != 'login_form') {
            $this->authenticate();
        }
    }

}
